package com.shensi.adminCollect.task.admin;

import com.shensi.adminCollect.controller.station.ClassSettlementController;
import com.shensi.adminCollect.domain.station.ClassSettlement;
import com.shensi.adminCollect.domain.station.Station;
import com.shensi.adminCollect.domain.station.vo.AutomaticClassInfoVo;
import com.shensi.adminCollect.mapper.station.AutomaticClassMapper;
import com.shensi.adminCollect.mapper.station.PayWayMapper;
import com.shensi.adminCollect.service.station.*;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Configuration
@EnableScheduling
public class AutomaticClassTask {
    private static final Logger log = LoggerFactory.getLogger(AutomaticClassTask.class);

    @Autowired
    protected AutomaticClassMapper automaticClassMapper;

    @Autowired
    protected PayWayMapper payWayMapper;


    @Autowired
    private AutomaticClassService automaticClassService;

    @Autowired
    private ClassSettlementController classSettlementController;

    @Autowired
    private IClassSettlementService classSettlementService;

    @Autowired
    private IPayWayService payWayService;

    @Autowired
    private IStationService stationService;



    @Autowired
    private IPayOrderAllService payOrderAllService;

  // cron = "0 0 0/1 * * ?"
    @XxlJob("automaticTask")
    private void AutomaticTask() throws ParseException, InterruptedException {
        List<AutomaticClassInfoVo> automaticClasses = automaticClassMapper.selectAutomaticClassBystatus(0);
        log.info("自动班结开始");
        if (ObjectUtils.isNotEmpty(automaticClasses)) {
            for (AutomaticClassInfoVo automaticClassInfoVo : automaticClasses) {
                try {

                Calendar calendar = Calendar.getInstance();
                //获取数据库时间
                String knotTime = automaticClassInfoVo.getKnotTime();
                String[] split = knotTime.split(":");
                String hour = split[0];
                //获取当前时间
                calendar.setTime(new Date());
                int currentHour = calendar.get(Calendar.HOUR_OF_DAY);//时（24小时制）
                String currentHour1= "";
                currentHour1 = String.valueOf(currentHour);
                if (currentHour < 10){
                    currentHour1 = "0" + currentHour;
                }


                Date currentDate = new Date();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

                String currentDateString = dateFormat.format(currentDate); // 当前日期的年月日字符串


                String completeDateString = currentDateString + " " + hour + ":" + "00" + ":" + "00";
                SimpleDateFormat completeDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date completeDate = completeDateFormat.parse(completeDateString);


                if (hour.equals(currentHour1)) {
                    log.info("自动班结 ————————————————油站id：{}，班结时间：{},用户id：{}", automaticClassInfoVo.getStationId(), automaticClassInfoVo.getKnotTime(), automaticClassInfoVo.getUserId());
                    ClassSettlement classSettlement = new ClassSettlement();
                    classSettlement.setStationId(automaticClassInfoVo.getStationId());
                    classSettlement.setClassEndTime(completeDate);
//                classSettlement.setClassStartTime(automaticClassInfoVo.getKnotTime());
                    classSettlement.setCreateUser(automaticClassInfoVo.getUserId());
                    classSettlement.setCreateName(automaticClassInfoVo.getUserName());
//                    automaticClassService.createClassSe(classSettlement);
                    classSettlementController.createClassSe(classSettlement);
                }
                } catch (Exception e) {

                    e.printStackTrace();
                }

            }
        }
    }

    // cron = 0 0 0/1 * * ?
    @XxlJob("autoPayWayTask")
    private void AutoPayWayTask() throws ParseException, InterruptedException {
//        List<AutoPayWayInfo> autoPayWayInfos =  payWayMapper.selectAutoPayWay();
        payWayService.selectAutoPayWay(0,0L,"0");
    }

    // cron = 0 0 0 * * ? 每天凌晨 0 点执行定时任务
    @XxlJob("payOrderAll")
    public void payOrderAll(){
        payOrderAllService.addNewPayOrder();
    }


    // cron = 0 0 0 * * ? 每天凌晨 0 点执行定时任务
    @XxlJob("setUsPayWay")
    public void setUsPayWay(){
        payWayService.selectMnoAllRateList();

    }

    // cron = 0 0 8 * * ? 确认日经营报表每天 8 点执行定时任务
    @XxlJob("createClassReportOnEight")
    public void createClassReportOnEight(){
        Station checkStation = new Station();
        checkStation.setStatus("1");
        checkStation.setDeleteStatus("0");
        List<Station> stationList=  stationService.selectStationList(checkStation);
        for (Station station:stationList){
            try {
                classSettlementService.insertBusinessDailyReport(station.getStationId(), 2, "1", null, null);
            } catch (Exception e) {

                e.printStackTrace();
            }
        }

    }

    // cron = 0 0 0 * * ? 确认日经营报表每天凌晨 0 点执行定时任务
    @XxlJob("createClassReport")
    public void createClassReport(){
        Station checkStation = new Station();
        checkStation.setStatus("1");
        checkStation.setDeleteStatus("0");
        List<Station> stationList=  stationService.selectStationList(checkStation);
        for (Station station:stationList){
            try {
                classSettlementService.insertBusinessDailyReport(station.getStationId(),0,"1",null,null);
            } catch (Exception e) {

                e.printStackTrace();
            }
        }

    }
}
